<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .login-box {
            box-sizing: border-box;
            margin: 50px;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #ecefff;
            width: 400px;
            height: 600px;
            padding-top: 10px;


        }

        .text {
            box-sizing: border-box;
            margin: 50px;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: white;
            width: 350px;
            border-radius: 10px;
            /* height: 600px; */

        }

        .login-box .title {
            /* font-weight: bold; */
            line-height: 80px;
            text-align: center;
        }

        .login-box .block {
            width: 300px;
            margin-bottom: 30px;
        }

        .login-box .block input {
            height: 40px;
            padding-left: 20px;
            /* border: 1px solid #ccc; */
            border-radius: 5px;
            width: 100%;
            outline: none;
            box-sizing: border-box;
            background-color: #f7f7f7;
            border: 0px;
        }

        .login-box .block:nth-child(5) input {
            background: linear-gradient(to right, #4b72ff, #8399ff);
        }
    </style>
</head>

<body>
    <div class="login-box">
        <img src="./al1.jpg" alt="">
        <div class="text">
            <div class="title">注册新账号</div>
            <div class="block">
                <input type="text" placeholder="请输入账号">
            </div>
            <div class="block">
                <input type="password" placeholder="请输入密码">
            </div>
            <div class="block">
                <input type="text" placeholder="确认密码">
            </div>
            <div class="block">
                <input type="button" value="注册">
            </div>
        </div>

        <img src="./al2.jpg" alt="">

    </div>

    <script src="./ajax.js"></script>
    <script>

        //获取五个input输入框
        var inputs = document.querySelectorAll('.login-box .block input');
        // console.log(inputs);

        //给button绑定点击事件
        inputs[3].onclick = function () {
            //对表单内容做校验( 为空校验, 格式校验 )
            if (inputs[0].value && inputs[1].value == inputs[2].value) {
                if (/^1[3-9]\d{9}$/ || /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(inputs[0].value) && /^\d{4}\w$/.test(inputs[1].value)) {
                    //通过校验, 发起登陆请求
                    ajax('get', 'https://yantianfeng.com/api/register', {
                        username: inputs[0].value,
                        password: inputs[1].value,
                        password2: inputs[2].value,
                    }).then( (data) =>{
                        console.log(data);
                    })
                }

                alert('注册成功')

            } else {
                alert('注册失败')
            }
        }


    </script>

</body>

</html>